<?php
/**
 * Created by PhpStorm.
 * User: xinfeng
 * Date: 2017/8/30
 * Time: 11:02
 */

$ds = DIRECTORY_SEPARATOR;
$dir = realpath(dirname(__FILE__) . $ds .'..');
include_once($dir.'/models/UseRedis.php');
include_once($dir.'/models/UseMySql.php');

class User {



	/**
	 * 获取用户信息
	 * @param array $info
	 * @return array
	 */
	public function getUserInfoList($info = array())
	{
		$mysql = new UseMySql();
		return $mysql->getUserInfoList(array('userList'=>$info['userList']));
	}


	/**
	 *
	 * @param array $info
	 * @return array
	 */
	public function login($info=array())
	{
		$return = array();
		$return['error'] = 0;
		if (empty($info['mobile']) || empty($info['code'])) {
			$return['error'] = 1;
			$return['message'] = '数据不合法';
			return $return;
		}
		$mysql = new UseMysql();
		$redis = new UseRedis();

		//校验验证码
		$data = array(
			'db' => 0,
			'key' => 'sendMsg:'.$info['mobile'],
		);
		$res = $redis->getRedis($data);
//		$redis->delRedis($data);
		if ($res != $info['code']) {
			$return['error'] = 2;
			$return['message'] = '验证码错误';
			return $return;
		}
		//登录注册
		$res = $mysql->getUserInfoByTel($info);
		if ($res['error'] == 0 && empty($res['message']['dataList'])) {
			$mysql->register($info['mobile']);
		}
		$mysql->login($info['mobile']);
		$return['data'] = $mysql->getUserInfoByTel($info);

		return $return;
	}

	public function sendMsg($mobile='')
	{
		$return = array();
		$return['error'] = 0;
		include '../lib/alidayu/send_check_code.php';
		$code = rand(1000, 9999);
		if (!empty($mobile)) {
//			$res = send_check_code($code, $mobile);
//			if ($res['msgid'] != 0) {
//				$return['error'] = 1;
//				$return['message'] = '短信验证码发送失败';
//				return $return;
//			}

			$data = array(
				'db' => 0,
				'key' => 'sendMsg:'.$mobile,
				'value' => $code,
				'exptime' => 300,
			);
			$redis = new UseRedis();
			$redis->setRedis($data);
		}
		return $return;
	}
}

//$user = new User();
////$a = $user->sendMsg('18372551560');
////var_dump($a);
//
//$data = array(
//	'mobile' => '18372551560',
//	'code'   => '1234'
//);
//$a = $user->login($data);
//var_dump($a);